package com.jcp.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jcp.api.Sample;
import com.jcp.dao.ParkMapper;
import com.jcp.entity.Park;
import com.jcp.entity.Record;
import com.jcp.entity.Users;
import com.jcp.service.carOutAndOn;

/**
 * 车辆进出的控制器
 * @author 朱耿封
 *
 */
@Controller
public class CarOutAndOnController {
	//车辆进出服务层的对象依赖注入
	@Autowired
	private carOutAndOn coao;
	
	@Autowired
	private ParkMapper pm;
	

	public String selectByCarIn(HttpServletRequest hsr,Model model,@RequestParam String base) {
		
		
		//判断用户是否注册的功能
		String tishi = coao.selectByCarNumber(base);
		hsr.getSession().setAttribute("ti", tishi);

		hsr.getSession().setAttribute("car",pm.selectPark().get(0).getOrdinary_park_space());
		return "jinru";
	}
	public String selectByCarOut(String carNumber,HttpServletRequest hsr,Model model) {
		
		//判断用户是否注册的功能
		String tishi = coao.selectByCarOut(carNumber);
//		return "forward:Tip.jsp";
		if(tishi.length()<7) {

			hsr.getSession().setAttribute("money111",tishi);
			return "chuqu";
		}else {
			hsr.getSession().setAttribute("ti", tishi);
			hsr.getSession().setAttribute("car",pm.selectPark().get(0).getOrdinary_park_space());
			return "youjinru";
		}
	}
	@PostMapping(value="onAndout",produces="application/json;charset=utf-8")
	@ResponseBody
	public String selectByOutAndIn(HttpServletRequest hsr,Model model,@RequestParam String base) {
		System.out.println("图片Base64编码 : " + base);
		String carNumber = Sample.scan(base);
		System.out.println("车牌 ："+carNumber);
		Record record = coao.selectByCarOutandIn(carNumber);
		System.out.println("record :"+record);
		if(record==null) {
			String message = selectByCarIn(hsr,model,carNumber);
			return "jinru";
		}else if(record.getOut_time()==null) {
			
			String message = selectByCarOut(carNumber,hsr,model);
			System.out.println("message : "+message);
			return message;
		}else {
			return "欢迎光临";
		}
		
		
	}
	@PostMapping(value="SelectByCheWei")
	@ResponseBody
	public String selectByCheWEI() {
		
		return pm.selectPark().get(0).getOrdinary_park_space().toString();
		
	}
}
